package cn.cmcc.online.smsapi.ui.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by ZhangYifan on 2017/6/15.
 */

public abstract class ItemRepertoryBaseImpl<T> implements ItemRepertory<T>{
    private List<T> tableRowList;

    public ItemRepertoryBaseImpl() {
        tableRowList = new ArrayList<>();
    }

    public abstract T createItem();

    public T getItemFromRepertort() {
        return tableRowList.size() > 0 ? tableRowList.remove(0) : null;
    }

    public T getOrCreateItem() {
        return tableRowList.size() > 0 ? tableRowList.remove(0) : createItem();
    }

    public List<T> getAllItemsFromRepertory() {
        List<T> list = new ArrayList<>(tableRowList);
        tableRowList.clear();
        return list;
    }

    public void recycleItem(T item) {
        tableRowList.add(item);
    }

    public void recycleCollectItems(Collection<T> items) {
        for (T item : items) {
            recycleItem(item);
        }
    }
}
